package com.wxb.common.weixin.service;

import com.wxb.common.core.utils.StringUtils;
import com.wxb.common.weixin.configure.WeiXinMpConfig;
import com.wxb.common.weixin.configure.WeiXinPayConfig;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;

@Configuration
@ConditionalOnClass(WxPayService.class) //引入WxPayService这个类 下面两个才会实例化
@EnableConfigurationProperties({WeiXinPayConfig.class, WeiXinMpConfig.class}) //注入WxPayProperties类
@RequiredArgsConstructor
public class WeiXinService {
    @Autowired
    private WeiXinPayConfig payConfig;

    @Autowired
    private WeiXinMpConfig mpConfig;

    /**
     * 获取小程序WxMaService
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public WxMaService wxMaService() {
        //实例这个 WxMaConfig 设置参数
        WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
        wxMaConfig.setAppid(StringUtils.trimToNull(this.payConfig.getAppId()));
        wxMaConfig.setSecret(StringUtils.trimToNull(this.payConfig.getAppSecret()));

        WxMaService wxMaService = new WxMaServiceImpl();
        //设置配置文件
        wxMaService.setWxMaConfig(wxMaConfig);
        return wxMaService;
    }

    /**
     * 获取小程序WxPayService
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxPayService() {
        ////实例WxPayConfig 设置参数
        //WxPayConfig payConfig = new WxPayConfig();
        //payConfig.setAppId(StringUtils.trimToNull(this.payConfig.getAppId()));
        //payConfig.setMchId(StringUtils.trimToNull(this.payConfig.getMchId()));
        //payConfig.setMchKey(StringUtils.trimToNull(this.payConfig.getMchKey()));
        //// payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
        //// payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
        //payConfig.setKeyPath(StringUtils.trimToNull(this.payConfig.getKeyPath()));
//
        //// 可以指定是否使用沙箱环境
        //payConfig.setUseSandboxEnv(false);
//
        //WxPayService wxPayService = new WxPayServiceImpl();
        ////设置配置文件
        //wxPayService.setConfig(payConfig);
        //return wxPayService;
        return null;
    }

    /**
     * 获取公众号WxMpService
     *
     * @return
     */
    @Bean
    public WxMpService wxMpService(){
        //实例WxMpConfig 设置参数
        WxMpDefaultConfigImpl mpConfig = new WxMpDefaultConfigImpl();
        mpConfig.setAppId(StringUtils.trimToNull(this.mpConfig.getMpAppId()));
        mpConfig.setSecret(StringUtils.trimToNull(this.mpConfig.getMpAppSecret()));

        WxMpService wxMpService = new WxMpServiceImpl();
        //设置配置文件
        wxMpService.setWxMpConfigStorage(mpConfig);
        return wxMpService;
    }
}
